package com.example.im.gray;

import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *  设置参数切面
 */
@Component
@Aspect
public class GrayParameterAspect {

    private static final String GRAY_HEADER = "gray_header";
    private static final String GRAY_VALUE = "gray";

    @Pointcut("execution(* com.example.im.controller..*Controller*.*(..))")
    public void anyMethod(){

    }

    @Before(value = "anyMethod()")
    public void before(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String gray = request.getHeader(GRAY_HEADER);
        if(StringUtils.equals(gray,GRAY_VALUE)){
            Map<String,String> map = new HashMap<>();
            map.put(GRAY_HEADER,GRAY_VALUE);
            GrayRibbonParamater.put(map);
        }
    }
}
